package com.sqx.modules.health.controller;

import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.modules.app.utils.JwtUtils;
import com.sqx.modules.health.common.Constants;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.*;
import com.sqx.modules.health.service.IAppUserDietRecordRemarkService;
import com.sqx.modules.health.service.IAppUserDietRecordService;
import com.sqx.modules.health.service.IUserService;
import com.sqx.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 患者康复方案记录表 前端控制器
 * </p>
 *
 * @author hsx
 * @since 2022-09-13
 */
@Slf4j
@Api(value = "方案记录表", tags = "", description = "方案记录")
@RestController
@RequestMapping("/health/app/app_diet_record")
public class AppUserDietRecordController {
    @Autowired
    private IAppUserDietRecordService userDietRecordService;

    @Autowired
    private IAppUserDietRecordRemarkService userDietRecordRemarkService;

    @Autowired
    private IUserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    @PostMapping("/add")
    @ApiOperation(value = "添加方案记录", notes = "添加方案记录")
    public Result add(@RequestParam Map<String, String> params) {
        String token = params.get("token");
        String img = params.get("img");
        String type = params.get("type");

        if (StrUtil.isBlank(token) || StrUtil.isBlank(img) || StrUtil.isBlank(type)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        //String uid = JWT.decode(token).getAudience().get(0);
        String uid = jwtUtils.getUserIdByToken(token);
        AppUserDietRecord appUserDietRecord = new AppUserDietRecord();
        appUserDietRecord.setImg(img);
        appUserDietRecord.setUid(Long.valueOf(uid));
        appUserDietRecord.setType(type);
        if (userDietRecordService.save(appUserDietRecord)) {
            return Result.success("操作成功");
        } else {
            return Result.error(Constants.CODE_400, "操作失败");
        }
    }

    @PostMapping("/addRemark")
    @ApiOperation(value = "App患者端转借设备前检查", notes = "App患者端转借设备前检查")
    public Result addRemark(@RequestParam Map<String, String> params) {
        String token = params.get("token");
        String content = params.get("content");
        String recordId = params.get("recordId");

        if (StrUtil.isBlank(token) || StrUtil.isBlank(content) || StrUtil.isBlank(recordId)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        //String uid = JWT.decode(token).getAudience().get(0);
        String uid = jwtUtils.getUserIdByToken(token);
        SysUserEntity user = userService.getById(uid);

        AppUserDietRecordRemark appUserDietRecordRemark = new AppUserDietRecordRemark();
        appUserDietRecordRemark.setUid(Long.valueOf(uid));
        appUserDietRecordRemark.setUname(user.getNickName());
        appUserDietRecordRemark.setRecordid(Long.valueOf(recordId));
        appUserDietRecordRemark.setContent(content);
        if (userDietRecordRemarkService.save(appUserDietRecordRemark)) {
            return Result.success("操作成功");
        } else {
            return Result.error(Constants.CODE_400, "操作失败");
        }
    }

    @PostMapping("/remarkList")
    @ApiOperation(value = "评论列表", notes = "评论列表")
    public Result remarkList(@RequestParam Map<String, String> params) {
        String token = params.get("token");
        String curruid = params.get("uid");
        String type = params.get("type");
        if (StrUtil.isBlank(token) || StrUtil.isBlank(curruid) || StrUtil.isBlank(type)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        //String uid = JWT.decode(token).getAudience().get(0);
        String uid = jwtUtils.getUserIdByToken(token);
        SysUserEntity user = userService.getById(uid);
        if (1 == user.getType()) {
            uid = curruid;
        }
        List<AppUserDietRecord> userDietRecordList = userDietRecordService.list(
                new QueryWrapper<AppUserDietRecord>()
                        .eq(1 == user.getType(),"uid", curruid)
                        .eq(1 != user.getType(),"uid", uid)
                        .eq("type", type)
                        .orderByDesc("createtime")
                );
        for (AppUserDietRecord record : userDietRecordList) {
            List<AppUserDietRecordRemark> recordRemarkList = userDietRecordRemarkService.list(
                    new QueryWrapper<AppUserDietRecordRemark>()
                            .eq("recordid", record.getId()).orderByAsc("createtime")
            );
            record.setRemarkList(recordRemarkList);
        }

        return Result.success("操作成功", userDietRecordList);
    }

}

